#include <my_head.h>
#include <sys/select.h>
#include <sqlite3.h>

#include "server.h"
#include "mysqlite.h"

void do_client(int acceptfd, msg_t *msg);

int main(int argc, const char *argv[])
{   
    
    int s_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (s_fd < 0)
    {
        ERR_LOG("socket crete error");
    }

    struct sockaddr_in saddr;
    saddr.sin_family = AF_INET;
    saddr.sin_addr.s_addr = inet_addr(IP_ADD);
    saddr.sin_port = htons(PORT);

    if (bind(s_fd, (struct sockaddr *)&saddr, sizeof(saddr)) == -1)
    {
        ERR_LOG("bind error");
    }

    if (listen(s_fd, 5))
    {
        ERR_LOG("listen error");
    }
    //初始化数据库
    if(do_init_sqlite3() != 0)
    {
        ERR_LOG("sqlite3 openerr");
    }
    
    fd_set readfds;
    FD_ZERO(&readfds);
    fd_set readfds_temp;
    FD_ZERO(&readfds_temp);

    FD_SET(s_fd, &readfds);

    int max_fd = s_fd;
    int ret = 0;
    int acceptfd = 0;
    int bytes = 0;
    int i = 0;
    msg_t msg;
    printf("服务器启动...\n");
    while (1)
    {   
        readfds_temp = readfds;
        ret = select(max_fd + 1, &readfds_temp, NULL, NULL, NULL);
        if (ret == -1)
        {
            ERR_LOG("select error");
        }
        else if (ret == 0)
        {
            continue;
        }
        for (i = 3; i < max_fd+1 && ret != 0; i++)
        {
            if (FD_ISSET(i, &readfds_temp))
            {
                ret--;
                if (i == s_fd)
                {   
                    if ((acceptfd = accept(i, NULL, NULL)) == -1)
                    {
                        ERR_LOG("accept error");
                    }
                    printf("客户端[%d]连接成功\n", acceptfd);
                    FD_SET(acceptfd, &readfds);
                    max_fd = max_fd > acceptfd ? max_fd : acceptfd;
                }
                else
                {   
                    memset(&msg, 0, sizeof(msg_t));
                    if ((bytes = recv(i, &msg, sizeof(msg), 0)) == -1)
                    {
                        ERR_LOG("recv error");
                    }else if (bytes == 0 || msg.commd == '5')//5退出
                    {   
                        bytes = 0 ? printf("客户端[%d]断开了链接\n", i) : printf("客户端[%d]退出了\n", i);
                        FD_CLR(i, &readfds);
                        close(i);
                        continue;
                    }
                    do_client(i, &msg);
                }
            }
        }
    }

    close(s_fd);
    do_closeSql();
    return 0;
}
//处理事件
void do_client(int acceptfd, msg_t *msg){
    
    printf("接收到[%d]发来的的数据：%c\n", acceptfd, msg->commd);
    //0:登录、1:增、2:删、3:改、4:查、5:退出
    if (msg->commd == '0')
    {   
        do_login(msg);
        
    }else if (msg->commd == '1')
    {
        do_insert(msg);
        
    }else if (msg->commd == '2')
    {   
        do_delete(msg);
        
    }else if (msg->commd == '3')
    {
        do_modify(msg);
        
    }else if (msg->commd == '4')
    {   
       
        //如果查询到了就直接发送了不走下面的发送了
        if (do_query(acceptfd, msg) == 0)
        {   
            return;
        }
    }
    msg->search_count = 0;
    send(acceptfd, msg, sizeof(msg_t), 0);
}